package com.fastloan.jn.mgt.process.controller;

import com.fastloan.jn.mgt.process.core.ReturnData;
import com.fastloan.jn.mgt.process.exception.AppApproveException;
import com.fastloan.jn.mgt.process.model.JnAppApproveUser;
import com.fastloan.jn.mgt.process.model.vo.AppApproveVo;
import com.fastloan.jn.mgt.process.service.AppApproveService;
import com.fastloan.jn.mgt.process.service.AppApproveUserService;
import com.fastloan.jn.mgt.process.service.ProcessService;
import com.fastloan.safe.core.util.ManagerKit;
import com.fastloan.safe.mgt.org.service.CmuOrgService;
import com.fastloan.safe.mgt.user.service.CmuUserService;
import com.kawins.base.baseabs.BaseController;
import com.kawins.result.ResultEnum;
import com.kawins.result.ResultTo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 进件审批
 *
 * @author chenkai
 *         createTime 2017-02-2017/2/6 14:29
 */
@RestController
@RequestMapping("/api/web/applicationApproval")
public class ApplicationApprovalController extends BaseController<JnAppApproveUser> {
    /**
     * 进件审批流程
     */
    @Autowired
    private AppApproveService appApproveService;

    @Autowired
    private AppApproveUserService appApproveUserService;

    /**
     * 提交审核
     *
     * @param appApproveUser 审批信息
     * @param applicationId  进件id
     * @return 数据库变记录
     */
    @PostMapping("/{applicationId}")
    public ResultTo insert(@PathVariable("applicationId") String applicationId, JnAppApproveUser appApproveUser) {
        try {
            appApproveUser.setUserId(ManagerKit.getUserId());
            JnAppApproveUser entity = appApproveUserService.selectByPrimaryKey(appApproveUser.getId());
            if (entity.getApprovalConclution() == null) {
                ReturnData returnData = appApproveService.appApproveUserService(appApproveUser, applicationId);
                return new ResultTo().setData(returnData);
            }
            return new ResultTo(ResultEnum.OPERATION_FAILED, "无需重复审批进件");
        } catch (AppApproveException e) {
            e.printStackTrace();
            return new ResultTo(ResultEnum.OPERATION_FAILED, e.getMessage());
        }
    }

    /**
     * 根据进件id查询审批信息(签约信息,审批信息,客户信息)
     *
     * @param applicationId 进件id
     * @return 审批信息
     */
    @GetMapping("/{applicationId}")
    public ResultTo queryByAppId(@PathVariable("applicationId") String applicationId) {
        Map<String, Object> map = appApproveUserService.queryApproval(applicationId, ManagerKit.getUserId());
        return new ResultTo().setData(map);
    }

    /**
     * 查询待审核分页信息
     *
     * @param applicationStatus 进件状态
     * @return 分页信息
     */
    @RequestMapping("/pageList/{applicationStatus}")
    public ResultTo pageList(@PathVariable("applicationStatus") int applicationStatus) {
        String userId = ManagerKit.getUserId();
        Map<String, Object> map = new HashMap<>();
        map.put("userId", userId);
        map.put("applicationStatus", applicationStatus);
        //  map.put("customerType", ShiroKit.getOrgType());
        JnAppApproveUser approveUser = new JnAppApproveUser();
        approveUser.setUserId(userId);
        List<JnAppApproveUser> approveUsers = appApproveUserService.select(approveUser); //判断是否为审贷会审批
        for (JnAppApproveUser user : approveUsers) {
            if (user.getApprovalReviewId() != null) {
                map.put("approvalReviewId", user.getApprovalReviewId());
            }
        }
        return dataPage("queryApproval", map);
    }

    /**
     * 获取进件审批链
     *
     * @param applicationId 进件id
     * @return 审批链
     */
    @GetMapping("/getApproveChain/{applicationId}")
    public ResultTo getApproveChain(@PathVariable("applicationId") String applicationId, @RequestParam(defaultValue = "0") int flag) {
        try {
            List<AppApproveVo> approveChain = appApproveService.getApproveChain(applicationId, flag);
            return new ResultTo().setData(approveChain);
        } catch (AppApproveException e) {
            e.printStackTrace();
            return new ResultTo(ResultEnum.OPERATION_FAILED, e.getMessage());
        }
    }

    /**
     * 验证是否已签约
     *
     * @param applicationId   进件id
     * @param approveReviewId 审贷会id
     * @return 是否签约
     */
    @GetMapping("/{applicationId}/{approveReviewId}")
    public ResultTo queryExamineApproving(@PathVariable("applicationId") String applicationId, @PathVariable("approveReviewId") String approveReviewId) {
        Boolean aBoolean = appApproveService.queryExamineApproving(applicationId, approveReviewId);
        return new ResultTo().setData(aBoolean);
    }


}
